/**
 * 
 */
package es.aradesh.ministryassist.gui.visit;

import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import es.aradesh.ministryassist.R;
import es.aradesh.ministryassist.db.DbAdapter;
import es.aradesh.ministryassist.gui.ministry.AddEditActivity;
import es.aradesh.ministryassist.gui.ministry.HomeDetailsActivity;
import es.aradesh.ministryassist.logic.ministry.Home;
import es.aradesh.ministryassist.logic.ministry.MinistryElement;
import es.aradesh.ministryassist.logic.visit.Revisit;
import es.aradesh.ministryassist.logic.visit.RevisitBuilder;

/**
 * @author xmorales
 * 
 */
public class RevisitListActivity extends ListActivity {


	private String[] defaultMsg;
	private DbAdapter mDb;
    private MinistryElement contextPointer;
	private boolean listVoid;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.ListActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		mDb = new DbAdapter(this);
		mDb.open();
		defaultMsg = new String[] { this.getString(R.string.noElements) };
		
		
		fillRevisits();

		// Button OnClick() Definitions
		Button addButton = (Button) findViewById(R.id.addButton);
		Button backButton = (Button) findViewById(R.id.backButton);
		registerForContextMenu(getListView());
		
		addButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				Intent i = new Intent(view.getContext(), AddEditActivity.class);
				i.putExtra(MinistryElement.OPERATION,
						MinistryElement.ACTIVITY_EDIT_REVISIT);
				startActivityForResult(i,
						MinistryElement.ACTIVITY_EDIT_REVISIT);
			}
		});
		backButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
					Intent mIntent = new Intent();
					setResult(RESULT_OK, mIntent);
					finish();
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onActivityResult(int, int,
	 *      android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		fillRevisits();
//		switch (requestCode) {
//		case MinistryElement.ACTIVITY_EDIT_TERRITORY:
//			fillTerritories();
//			break;
//		case MinistryElement.ACTIVITY_EDIT_STREET:
//			fillStreets();
//			break;
//		case MinistryElement.ACTIVITY_EDIT_HOME:
//		case MinistryElement.ACTIVITY_SHOW_HOME:
//			fillHomes();
//			break;
//		}

	}

	private void fillRevisits() {
		//viewLayout = VIEW_TERRITORY;
		List<Revisit> revisits = RevisitBuilder.getRevisitsList(this.mDb);

		if (revisits.isEmpty()) {
			// Show a default message
			listVoid = true;
			setListAdapter(new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_1, defaultMsg));
		} else {
			listVoid = false;
			setListAdapter(new ArrayAdapter<Revisit>(this,
					android.R.layout.simple_list_item_1, revisits));
		}
		// Change title and button description
		((Button) findViewById(R.id.addButton)).setText(R.string.addrevisit);
		//TextView title = (TextView) findViewById(R.id.TextTerritoryTitle);
		//title.setText(R.string.territoryTitle);
		//this.setTitle(R.string.territoryTitle);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		// Object o = l.getItemAtPosition(position);
		Intent i = null;
		if (!l.getItemAtPosition(position).equals(defaultMsg[0])) {
			i = new Intent(v.getContext(), HomeDetailsActivity.class);
			i.putExtra(BaseColumns._ID, ((Home) l
					.getItemAtPosition(position)).getId());
			startActivityForResult(i, MinistryElement.ACTIVITY_SHOW_HOME);
			
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onContextItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		Intent i = new Intent(this, AddEditActivity.class);
		switch (item.getItemId()) {
		case MinistryElement.MENU_EDIT:
			i.putExtra(MinistryElement.OPERATION,
					contextPointer.getActivityEditType());
			i.putExtra(BaseColumns._ID, contextPointer.getId());
			startActivityForResult(i, contextPointer.getActivityEditType());
			break;
		case MinistryElement.MENU_DELETE:
			contextPointer.delete(mDb);
			onActivityResult(contextPointer.getActivityEditType(), RESULT_OK, i);
			break;
		}
		return super.onContextItemSelected(item);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreateContextMenu(android.view.ContextMenu,
	 *      android.view.View, android.view.ContextMenu.ContextMenuInfo)
	 */
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		if (!listVoid) {
			AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
			menu.add(0, MinistryElement.MENU_EDIT, 0, R.string.edit);
			menu.add(0, MinistryElement.MENU_DELETE, 1, R.string.delete);
			contextPointer = (MinistryElement) ((ListView) v)
					.getItemAtPosition(new Long(info.id).intValue());
			super.onCreateContextMenu(menu, v, menuInfo);
		}
	}

}
